import navigatorUserAgent from "@/utils/navigatorUserAgent";
function setupWebViewJavascriptBridge(callback: Function) {
  if (window.WebViewJavascriptBridge) {
    return callback(window.WebViewJavascriptBridge)
  }
  if (window.WVJBCallbacks) {
    return window.WVJBCallbacks.push(callback)
  }
  window.WVJBCallbacks = [callback]
  let WVJBIframe = document.createElement('iframe')
  WVJBIframe.style.display = 'none'
  WVJBIframe.src = 'https://__bridge_loaded__'
  document.documentElement.appendChild(WVJBIframe)
  setTimeout(() => {
    document.documentElement.removeChild(WVJBIframe)
  }, 0)
}
export default {
  callhandler(name: any, data: any, callback: any) {
    if (navigatorUserAgent() == "android") {
      if (!window.WebViewJavascriptBridge) return;
      if (!window.WebViewJavascriptBridge[name]) return;

      if (name == "getUserInfo") {
        window.userInfoCallBack = (res: any) => {
          if (typeof res == "string") {
            callback(JSON.parse(res))
          } else {
            callback(res)
          }
          window.userInfoCallBack = ""
        }
      } else if (name == "getPageArgs") {
        window.PageArgsCallBack = (res: any) => {
          if (typeof res == "string") {
            callback(JSON.parse(res))
          } else {
            callback(res)
          }
          window.PageArgsCallBack = ""
        }
      } else if (name == "verify") {
        window.verifyCallBack = (res: any) => {
          if (typeof res == "string") {
            callback(JSON.parse(res))
          } else {
            callback(res)
          }
          window.verifyCallBack = ""
        }
      } else if (name == "getuserAddress") {
        window.getuserAddressCallBack = (res: any) => {
          if (typeof res == "string") {
            callback(JSON.parse(res))
          } else {
            callback(res)
          }
          window.getuserAddressCallBack = ""
        }
      } else if (name == "getUserInfos") {
        window.userInfosCallBack = (res: any) => {
          if (typeof res == "string") {
            callback(JSON.parse(res))
          } else {
            callback(res)
          }
          window.userInfosCallBack = ""
        }
      } else if (name == "getGroupInfoByID") {
        window.GroupInfoCallBack = (res: any) => {
          if (typeof res == "string") {
            callback(JSON.parse(res))
          } else {
            callback(res)
          }
          window.GroupInfoCallBack = ""
        }
      } else {
        (window as any)[name] = (res: any) => {
          if (typeof res == "string") {
            callback(JSON.parse(res))
          } else {
            callback(res)
          }
          (window as any)[name] = "";
        }
      }
      if (data) {
        window.WebViewJavascriptBridge[name](JSON.stringify(data));
      } else {
        window.WebViewJavascriptBridge[name]();
      }
    } else {
      setupWebViewJavascriptBridge(function (bridge: any) {
        bridge.callHandler(name, data, callback)
      })
    }
  },
  registerhandler(name: any, callback: any) {
    setupWebViewJavascriptBridge(function (bridge: any) {
      bridge.registerHandler(name, function (data: any, responseCallback: any) {
        callback(data, responseCallback)
      })
    })
  }
}